package org.dc.agent.util;

import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil extends DateUtils {

	private static Logger log = Logger.getLogger(DateUtil.class);

	public static final String DEFAULT_DATE_FROMAT = "yyyy-MM-dd HH:mm:ss";

	public static Date parse(String strDate, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			log.error("ParseException", e);
		}
		return date;
	}

	public static Date parse(String strDate, String format, String classicName) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			if (!classicName.equals("lssice.ComputerUnregStatus")
					|| !classicName.equals("lssice.ProcessStatistics")) {
				// log.error(LangMsg.bind(LangMsg.BEAN_COPY_PROPERITY_ERROR,
				// format,strDate));
				// log.error("date parse error------"+classicName);
			}
		}
		return date;
	}

	public static String format(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static Calendar getCurrentCalendar() {
		TimeZone tz = TimeZone.getTimeZone("Asia/ShangHai");
		Calendar calendar = (Calendar) Calendar.getInstance(tz);
		return calendar;
	}

	public static Date getCurrentDate() {
		Calendar calendar = getCurrentCalendar();
		return calendar.getTime();
	}

	public static String getCurrentStrDate() {
		return format(getCurrentDate(), DEFAULT_DATE_FROMAT);
	}

	public static Date strToDate(String strDate) {
		Date date = null;
		if (strDate != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			try {
				date = sdf.parse(strDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	public static Integer DateToInt() {
		Date date = getCurrentDate();
		return DateToInt(date);
	}

	public static Integer DateToInt(Date date) {
		String dateStr = format(date, "yyyyMMdd");
		return Integer.valueOf(dateStr);
	}

	public static String converStrtime2Double(String timestr) {

		DateFormat dateTimeInstance = SimpleDateFormat.getDateTimeInstance();
		Date datee;
		String ss = "";
		try {
			datee = dateTimeInstance.parse(timestr);
			ss = "" + datee.getTime();

		} catch (ParseException e) {

			e.printStackTrace();
		}

		return ss;
	}

}
